package internationalenergytradingplatform.dao;

import com.github.pagehelper.Page;
import internationalenergytradingplatform.VO.OrderPageQueryVO;
import internationalenergytradingplatform.VO.PDFDataVO;
import internationalenergytradingplatform.dto.OrderPageQueryDTO;
import internationalenergytradingplatform.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {

    /**
     *
     * @author yingfeng
     * @date 2024/3/27 17:08
     订单分页查询
     */
    Page<OrderPageQueryVO> orderPageQuery(OrderPageQueryDTO orderPageQueryDTO);

    /**
     *
     * @author yingfeng
     * @date 2024/3/27 17:08
    管理员的订单分页查询
     */
    Page<OrderPageQueryVO> orderPageQueryAdmin(OrderPageQueryDTO orderPageQueryDTO);

    /**
     *
     * @author yingfeng
     * @date 2024/3/27 17:08
     可接订单分页查询（运输商）
     */
    Page<OrderPageQueryVO> orderPageQueryAvailable(OrderPageQueryDTO orderPageQueryDTO);

    /**
     *
     * @author yingfeng
     * @date 2024/3/27 17:12
     */
    Page<OrderPageQueryVO> orderPageQueryProcess (OrderPageQueryDTO orderPageQueryDTO);

    /**
     * 根据id查询order的全部信息
     */
    @Select("select * from order_tb where id = #{id}")
    Order getOrderById(Integer id);

    /**
     * 加入1-2流程的整局图片，并将流程升级为2
     */
    @Update("update order_tb set supplier_evidence = #{imgUrl}, status = 2 where id = #{id}")
    void updateStatus(@Param("imgUrl") String imgUrl, @Param("id") Integer id);

    /**
     * 新增订单
     */
    @Insert("insert into order_tb values (null, #{orderTime}, #{supplier}, #{buyer}, #{status}, #{abnormal}, #{commodity}, " +
            "#{number}, #{transporter}, #{refundStatus}, #{supplierEvidence}, #{transporterEvidence}, #{buyerTele}, #{freight})")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    void addOrder(Order order);

    /**
     *更新订单状态由运输商运输中（2）改为待采购商收货（3）或者异常（5）
     */
    @Update("update order_tb set status = #{status}, abnormal = #{abnormal} ,transporter_evidence = #{transporterEvidence} where id = #{id}")
    void updateOrderStatus(@Param("abnormal") Integer abnormal, @Param("id") Integer id, @Param("status") Integer status, @Param("transporterEvidence") String transporterEvidence);

    /**
     * 运输商接单， 更新订单状态由待运输商接单（1）改为运输商运输中（2）
     */
    @Update("update order_tb set status = 2, transporter =#{companyId} where id = #{id}")
    void takeOrder(@Param("id") Integer id, @Param("companyId")  String companyId);

    /**
     * 获取发票信息
     */
    @Select("select su.company_name supplierName, su.address supplierAddress, su.tele supplierContact, " +
            "bu.mpany_name buyerName, bu.address buyerAddress, bu.tele buyerContact, " +
            "co.name commodityName, ord.number  commodityAmount, co.unit_inventory unitInventory, " +
            "co.unit_price unitPrice, co.price  commodityPrice " +
            "from order_tb ord left join commodity_tb co on ord.commodity = co.id " +
            "left join supplier_accountr_tb su on ord.supplier = su.tele " +
            "left join buyer_accountr_tb bu on ord.buyer = bu.tele " +
            "where ord.id = #{id}")
    PDFDataVO getInvoiceInfo(Integer id);

    /**
     *
     * @author yingfeng
     * @date 2024/4/30 23:09
     查询采购商的所有订单
     */
    @Select("select o.id, c.name, o.number, o.order_time, o.transporter, o.status, c.image1 from " +
            "order_tb o left join commodity_tb c on o.commodity = c.id " +
            "where buyer = #{buyer}")
    List<OrderPageQueryVO> getAllOrderByOwn(String buyer);

    /**
     *
     * @author yingfeng
     * @date 2024/5/1 15:02
     采购商签收订单
     */
    @Update("update order_tb set status = 4 where id = #{order}")
    void signForOrder(Integer order);
}
